package com.sljy.hzk.view;

import android.content.Context;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.sljy.hzk.entity.TaskDetailEntity;
import com.sljy.hzk.entity.ui.TaskOptionEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created  on 2018/7/2 0002
 *
 * @author 唐开阔
 * @describe 单选框 && 复选框 ---  目前只支持代码设置，不提供xml方式
 */
public class ChoiceLayout extends LinearLayout {
    //选项的总数
    private int itemNums;
    //当前选择item的坐标
    private int nowCheckPos = -1;
    //是否是单选
    private  boolean isSingleCheck = true;
    private boolean[] ischeckArr;
    private List<SmoothCheckBox> boxs;

    public ChoiceLayout(Context context) {
        this(context, null);
    }

    public ChoiceLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChoiceLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        boxs = new ArrayList<>();
    }

    /**
     * 绑定选项
     */
    public void bindOption(@NonNull List<TaskDetailEntity.Subitem.Item> options) {
        itemNums = options.size();
        ischeckArr = new boolean[itemNums];
        for (int i = 0; i < itemNums; i++) {
            TaskDetailEntity.Subitem.Item item = options.get(i);
            //恢复状态
            if (isSingleCheck && item.isCheck()){
                nowCheckPos = i;
            }
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_task_option, this, false);
            SmoothCheckBox box = view.findViewById(R.id.box);
            TextView textView = view.findViewById(R.id.tv_item);
            box.setChecked(item.isCheck());
            textView.setText(item.getValue());
            boxs.add(box);
            int finalI = i;
            box.setOnCheckedChangeListener((checkBox, isChecked) -> {
                item.setCheck(isChecked);
                ischeckArr[finalI] = isChecked;
                //单选切选中
                if (isSingleCheck &&  isChecked  && nowCheckPos != finalI){
                    if ( nowCheckPos != -1) {
                        boxs.get(nowCheckPos).setChecked(false);
                    }
                    nowCheckPos = finalI;
                }
            });
            addView(view);
        }
    }

    public void setSingleCheck(boolean singleCheck) {
        isSingleCheck = singleCheck;
    }
    @Override
    public Parcelable onSaveInstanceState() {
        Log.d("ChoiceLayout", ">>>onSaveInstanceState---" );
        return super.onSaveInstanceState();
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        Log.d("ChoiceLayout", ">>>onRestoreInstanceState---" );
        super.onRestoreInstanceState(null);
    }
}
